﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Drawing;

namespace L9SubSet.lwf
{
	public class SystemTubeReader
	{
		private Int32 m_TubeSize;					// 灯具的基本直径（单位：像素）
		private Int32 m_TubeSpace;					// 灯具的基本间距（单位：像素）
		private Point m_SystemTube;					// 系统的灯具分辨率（单位：灯具）

		private List<SubTubeData> m_SubTube;		// 控制器对应的数组
		private List<PortTubeData> m_NoSubTube;		// 未使用的控制器数组
		private PortTubeData m_NoConTube;			// 孤立的控制器数组
		private List<ColorData> m_ColorTube;		// 灯具类型列表

		public SystemTubeReader()
		{
			m_SubTube = new List<SubTubeData>();
			m_NoSubTube = new List<PortTubeData>();
			m_NoConTube = new PortTubeData();
			m_ColorTube = new List<ColorData>();
		}

		// 从 xml 文件中读主数据
		public List<SubTubeData> readSystemTubeDataFormXml(string xmlFile)
		{
			m_SubTube.Clear();
			m_NoSubTube.Clear();
			m_NoConTube.Clear();
			m_ColorTube.Clear();

			int read_end = 0;
			
			XmlTextReader reader = new XmlTextReader(xmlFile);
			while (reader.Read())
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.Element:
						if (reader.Name == "SystemTubeDataStore")
							readFromXml(reader);
						break;
					case XmlNodeType.Text:
						break;
					case XmlNodeType.EndElement:
						read_end = 1;
						break;
					case XmlNodeType.Comment:
						break;
				}

				if (read_end == 1)
				{
					break;
				}
			}

			reader.Close();

			return m_SubTube;
		}

		// 读 xml 文件
		public int readFromXml(XmlTextReader rrr)
		{
			int read_end = 0;

			while (rrr.Read())
			{
				switch (rrr.NodeType)
				{
					case XmlNodeType.Element:
						if (rrr.Name == "m_TubeSize")
							m_TubeSize = LwfXmlRead.ReadFromXml(rrr, m_TubeSize);
						else if (rrr.Name == "m_TubeSpace")
							m_TubeSpace = LwfXmlRead.ReadFromXml(rrr, m_TubeSpace);
						else if (rrr.Name == "m_SystemTube")
							m_SystemTube = LwfXmlRead.ReadFromXml(rrr, m_SystemTube);
						else if (rrr.Name == "m_SubTube")
							m_SubTube = LwfXmlRead.ReadFromXml(rrr, m_SubTube);
						else if (rrr.Name == "m_NoSubTube")
							m_NoSubTube = LwfXmlRead.ReadFromXml(rrr, m_NoSubTube);
						else if (rrr.Name == "m_NoConTube")
							m_NoConTube = LwfXmlRead.ReadFromXml(rrr, m_NoConTube);
						else if (rrr.Name == "m_ColorTube")
							m_ColorTube = LwfXmlRead.ReadFromXml(rrr, m_ColorTube);
						break;

					case XmlNodeType.Text:
						break;

					case XmlNodeType.EndElement:
						read_end = 1;
						break;

					case XmlNodeType.Comment:
						break;
				}

				if (read_end == 1)
				{
					break;
				}
			}

			return 0;
		}
	}
}
